package cn.bugDance.domain.award.service.distribute.impl;

import cn.bugDance.domain.award.model.aggregate.GiveOutPrizesAggregate;
import cn.bugDance.domain.award.model.aggregate.UserAwardRecordAggregate;
import cn.bugDance.domain.award.model.entity.DistributeAwardEntity;
import cn.bugDance.domain.award.model.entity.UserAwardRecordEntity;
import cn.bugDance.domain.award.model.entity.UserCreditAwardEntity;
import cn.bugDance.domain.award.model.valobj.UserAwardRecordStateVO;
import cn.bugDance.domain.award.repository.IAwardRepository;
import cn.bugDance.domain.award.service.distribute.IDistributeAward;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("user_credit")
public class UserCreditAward implements IDistributeAward {

    @Resource
    private IAwardRepository awardRepository;


    @Override
    public void giveOutPrizes(DistributeAwardEntity distributeAwardEntity) {
        String userId = distributeAwardEntity.getUserId();
        String awardConfig = distributeAwardEntity.getAwardConfig();
        String orderId = distributeAwardEntity.getOrderId();

        UserAwardRecordEntity userAwardRecordEntity = new UserAwardRecordEntity();
        userAwardRecordEntity.setAwardState(UserAwardRecordStateVO.COMPLETED);
        userAwardRecordEntity.setOrderId(orderId);

        UserCreditAwardEntity userCreditAwardEntity = new UserCreditAwardEntity();
        userCreditAwardEntity.setUserId(userId);
        userCreditAwardEntity.setCreditAmount(Integer.valueOf(awardConfig));

        GiveOutPrizesAggregate giveOutPrizesAggregate = new GiveOutPrizesAggregate();
        giveOutPrizesAggregate.setUserId(userId);
        giveOutPrizesAggregate.setUserCreditAwardEntity(userCreditAwardEntity);
        giveOutPrizesAggregate.setUserAwardRecordEntity(userAwardRecordEntity);

        awardRepository.giveOutPrizes(giveOutPrizesAggregate);
    }
}
